package eo.builder.impl;

import java.io.IOException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import eo.builder.URLVisitor;
import eo.builder.util.IterationStep;

public class JARFileURLVisitor extends AbstractURLVisitor implements URLVisitor {

	public boolean canResolve(URL url) {
		if (url == null) {
			return false;
		}
		String path = url.getPath();
		return path.endsWith(".zip") || path.endsWith(".jar");
	}

	public void iterate(IterationStep<String> iteration, URL url)
			throws IOException {
		final ZipInputStream input = new ZipInputStream(url.openStream());
		try {
			ZipEntry entry = input.getNextEntry();
			while (entry != null) {
				entry = iteration.nextIteration(entry.getName()) ? input
						.getNextEntry() : null;
			}
		} finally {
			input.close();
		}
	}

}
